package com.shoukaiseki.test.spring.validator.plugin;

import com.alibaba.fastjson.JSONObject;
import com.shoukaiseki.test.spring.validator.model.ResultModel;
import org.hibernate.validator.internal.engine.ConstraintViolationImpl;
import org.hibernate.validator.internal.engine.path.NodeImpl;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Path;
import javax.validation.constraints.Min;
import javax.validation.metadata.ConstraintDescriptor;
import java.lang.annotation.Annotation;
import java.text.MessageFormat;
import java.util.Set;

/**
 * com.hkedou.orighere.buyersystem.plugin.ResultExceptionHandle <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  2018-11-17 18:11:53<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/

@ControllerAdvice
@EnableWebMvc
@Component
public class ResultExceptionHandle {

    private static Logger logger = LoggerFactory.getLogger("com.shoukaiseki.exception.handle");


    @ResponseBody
    @ExceptionHandler({Throwable.class})
    public ResultModel requestMissingServletRequest(HttpServletRequest request, Throwable e){
        logger.debug("----------------",e);
        ResultModel resultModel=new ResultModel();
        String msg="";
        if(e instanceof ConstraintViolationException) {
            ConstraintViolationException exception= (ConstraintViolationException) e;
            msg=e.getMessage();
            Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations();
            for (ConstraintViolation con:constraintViolations) {
                ConstraintViolationImpl as;
                con.getMessageTemplate();
                Path propertyPath = con.getPropertyPath();
                PathImpl path= (PathImpl) propertyPath;
                NodeImpl node = path.makeLeafNodeIterable();
                ConstraintDescriptor constraintDescriptor = con.getConstraintDescriptor();
                Annotation annotation = constraintDescriptor.getAnnotation();
                if(annotation instanceof Min){
                    logger.debug("annotation is Min");
                    msg="属性{0}的值{1}不能小于{2}";
                    resultModel.addStatusParams(node.getName(),con.getInvalidValue(),((Min) annotation).value());
                }
                logger.debug("annotation={}",annotation);
                System.out.println("aa");
            }
        }else {
//            result.setStatus(ResultException.DEFA_GROUP,ResultException.DEFA_ERROR);
            msg=e.getMessage();
        }
        try {

            String format = MessageFormat.format(msg, resultModel.getStatusParams().toArray());
            resultModel.setData(format);
        }catch (Throwable throwable){
            logger.error("",throwable);
        }
        resultModel.setMsg(msg);
        return resultModel;
    }
}


